package org.regotto.basics.dataStruct;

public class Stack {

    private int[] s = new int[1000];
    // 栈顶top
    private int t = 0;

    public void I(int x) {
        s[t++] = x;
    }

    public int T() {
        return t == 0 ? Integer.MAX_VALUE : s[t - 1];
    }

    public int D() {
        return s[t--];
    }

    String E() {
        return t == 0 ? "empty" : "non-empty";
    }

    // 最小栈 使用栈结构维护最小值, 以及 top 元素
    public static int[] getMinAndTop(Stack stack, int[] a) {
        int min = a[0], top;
        stack.I(min);
        for (int i = 0; i < a.length; i++) {
            stack.I(a[i] - min);
            if (a[i] < min) min = a[i];
        }
        // 输出栈顶元素
        if (stack.T() > 0) top = stack.T() + min;
        else top = min - stack.T();
        return new int[]{min, top};
    }

    public static void main(String[] args) {
        Stack stack = new Stack();
        stack.I(1);
        stack.I(2);
        stack.I(3);
        System.out.println(stack.T());
        stack.D();
        System.out.println(stack.T());
        System.out.println(stack.E());
    }

}

